import { types } from ".";

const defaultParams = {
  dft: 0,
  force: true,
  max: Infinity,
  min: -Infinity
};

export default class DirNumber {
  name = 'number';
  type = types.VALUE;

  constructor(params) {
    this.params = Object.assign({}, defaultParams, params);
  }

  with(value = '') {
    const { params } = this;

    if (params.force) {
      value = String(value).replace(/[^0-9]/g, '');
    }

    value = Number(value);

    if (isNaN(value)) {
      value = params.dft;
    }

    if (params.max) {
      value = Math.min(value, params.max);
    }

    if (params.min) {
      value = Math.max(value, params.min);
    }

    return value;
  }
}